#include <Ps3Controller.h>

#define REC_FRAMEMILLIS 500

unsigned long NextTransmitTime = 0;
void receive_ps3_Data();
int triangle,circle,cross,square;
int up,down,left,right;
int l1,l2,r1,r2;
int start;
char circle_pressed,square_pressed,triangle_pressed;
char CurDpad,CurDpad2;
String temp;
String check_command()
{
   if ( (millis() - NextTransmitTime) > REC_FRAMEMILLIS) {
   NextTransmitTime = millis();
   if(CurDpad=='f') {
      
      temp="1";   
      }
      if(CurDpad=='b'){
       temp= "3"; 
        
      }

      if(CurDpad=='l'){
      temp= "5";     
      } 
      if(CurDpad=='r') {
      temp= "4";   
      }
      if(CurDpad==0) {
      temp= "0"; 
      }
      if(up>240) {
      temp= "1"; 
      }
      if(right>240) {
      temp= "6"; 
      }
      if(left>240) {
      temp= "7"; 
      }
      if(down>240) {
      temp= "3"; 
      }
      if(triangle>240) {
      temp= "<"; 
      }
     if(circle>240) {
      temp= ";"; 
      }
      if(cross>240) {
      temp= "8"; 
      }
      if(square>240) {
      temp= "9"; 
      }
      if(l1>240) {
      temp= ":"; 
      }
      if(l2>240) {
      temp= ":"; 
      }
      if(r1>240) {
      temp= ":"; 
      }
      
      if(r2>240) {
      temp= ":"; 
      }
      if(start>240) {
      temp= "0"; 
      }
  
   }
  // Serial.println(temp);
   return temp;
}

void notify()
{
  int lx =(Ps3.data.analog.stick.lx);
  int ly =(Ps3.data.analog.stick.ly);


  int rx =(Ps3.data.analog.stick.rx);
  int ry =(Ps3.data.analog.stick.ry);

   up =(Ps3.data.analog.button.up);
   down =(Ps3.data.analog.button.down);
   left =(Ps3.data.analog.button.left);
   right =(Ps3.data.analog.button.right);

   triangle =(Ps3.data.analog.button.triangle);
   circle =(Ps3.data.analog.button.circle);
   cross =(Ps3.data.analog.button.cross);
   square =(Ps3.data.analog.button.square);

   l1 =(Ps3.data.analog.button.l1);
   r1 =(Ps3.data.analog.button.r1);
   l2 =(Ps3.data.analog.button.l2);
   r2 =(Ps3.data.analog.button.r2);
   
  //Serial.println(up);

  if (lx < -50 || left>240) {
    CurDpad = 'l';
  } else if (lx > 50 || right>240) {
    CurDpad = 'r';
  } else CurDpad=0;

  if (ly < -50 || up>240) {
    CurDpad = 'f';
  } else if (ly > 50 || down>240) {
    CurDpad = 'b';
  } else if(CurDpad==0) CurDpad = 0;
//Serial.println(CurDpad);

/****************************/
  if (rx < -50) {
    CurDpad2 = 'a';
  } else if (rx > 50) {
    CurDpad2 = 'd';
  } else CurDpad2=0;

  if (ry < -50) {
    CurDpad2 = 'w';
  } else if (ry > 50) {
    CurDpad2 = 's';
  } else if(CurDpad2==0) CurDpad2 = 0;
  if(CurDpad2!=0) start=255; else start=0;
//Serial.println(CurDpad2);

}

void onConnect()
{
  //Serial.println("Connected!.");
}

void onDisConnect()
{
  Serial.println("Ready.");

}





//B站找 ：arduino捣鼓��?欢迎大家评论区一起交流！如有不足请大家指��?共同进步��?
void ps3_init()
{
  
 
  Ps3.attach(notify);
  Ps3.attachOnConnect(onConnect);
  Ps3.attachOnDisconnect(onDisConnect);
  Ps3.begin();
  String address = Ps3.getAddress();

  Serial.print("The ESP32's Bluetooth MAC address is: ");
  Serial.println(address);
  Serial.println("Ready.");
  NextTransmitTime = millis();
}



//B站找 ：arduino捣鼓��?欢迎大家评论区一起交流！如有不足请大家指��?共同进步��?
